﻿#include "mywidget.h"
#include <QHBoxLayout>
#include <QApplication>
#include <QDesktopWidget>
#include <QQmlEngine>
#include <QQmlContext>
#include <QMenu>

constexpr int GLOBAL_OPEN_MOUSE_ENABLE{ 0 };
constexpr int GLOBAL_BACKGROUND_OPACITY{ 0 };
constexpr int GLOBAL_SYSTEM_MENU{ 1 };

MyWidget::MyWidget(QWidget *parent)
    : QWidget(parent), m_Pressed(false)
    , m_Speed(1.0), m_Dir(MoveDir_Up), m_BottomPos(0)
{
    m_view = new QQuickWidget(this);
    QHBoxLayout* layout = new QHBoxLayout(this);
    layout->addWidget(m_view);

    layout->setContentsMargins(0, 0, 0, 0);
    setLayout(layout);

    m_view->engine()->rootContext()->setContextProperty("widget",this);

    m_view->setSource(QUrl(QStringLiteral("qrc:/main.qml")));

    if(GLOBAL_BACKGROUND_OPACITY == 0)
    {
        this->setWindowFlags(this->windowFlags() /*| Qt::SubWindow*/ | Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint);//窗口置顶，无边框
        setAttribute(Qt::WA_TranslucentBackground);
        setAutoFillBackground(true);//填充背景

        QPalette palette;
        palette.setBrush(QPalette::Window, QBrush(QColor(Qt::transparent)));
        this->setPalette(palette);

        m_view->setClearColor(QColor(Qt::transparent));    //qml 透明
    }

    this->setFixedSize(300, 294);

    if(GLOBAL_OPEN_MOUSE_ENABLE != 0)
    {
        /*鼠标穿透*/
        SetWindowLong((HWND)winId(), GWL_EXSTYLE, GetWindowLong((HWND)winId(), GWL_EXSTYLE) |
                           WS_EX_TRANSPARENT | WS_EX_LAYERED);
    }

    if(GLOBAL_SYSTEM_MENU != 0)
    {
        setSystemMenu();
    }

    m_BottomPos = qApp->desktop()->height()-294-40;
    move(qApp->desktop()->width()-300, m_BottomPos);
//    move(qApp->desktop()->width()-300, qApp->desktop()->height()-294-40);

    m_thread = new QueryKeyThread(this);
    //connect(m_thread, &QueryKeyThread::queryResult, this, &MyWidget::queryResult);
    connect(m_thread, &QueryKeyThread::mousePointChanged, this, &MyWidget::mousePointChanged);
    m_thread->start();

    m_Timer = new QTimer(this);
    m_Timer->setInterval(20);
    connect(m_Timer, &QTimer::timeout, [=](){
        if(!m_Pressed){
            //move anim
            if(MoveDir_Down == m_Dir){
                int x = geometry().x();
                int y = geometry().y();
                move(x, y + m_Speed);

                if(y > m_BottomPos){
                    m_Dir = MoveDir_Up;
                }
            }
            else if(MoveDir_Up == m_Dir){
                int x = geometry().x();
                int y = geometry().y();
                move(x, y - m_Speed);

                if(y < 0){
                    m_Dir = MoveDir_Down;
                }
            }
        }
    });
    m_Timer->start();
}

void MyWidget::setSystemMenu()
{
    trayIcon = new QSystemTrayIcon(this);
    trayIcon->setIcon(QIcon(":/image/image/logo.ico"));

    QMenu *menu = new QMenu(this);

    QAction *action = menu->addAction("Exit");
    connect(action, &QAction::triggered, this, &MyWidget::systemExit);

    trayIcon->setContextMenu(menu);
    trayIcon->show();
}

void MyWidget::queryResult(QString name)
{
    (void)name;
    emit notifyMsg(gMouseX, gMouseY);
}


void MyWidget::mousePointChanged(int x, int y)
{
    emit notifyMousePos(x, y);
    emit notifyMouseScreenPos((float)x /  (float)qApp->desktop()->width(), (float)y / (float)qApp->desktop()->height());
    //qDebug()<<__FUNCTION__<<x<<y;
}

void MyWidget::systemExit(bool b)
{
    (void)b;
    qApp->quit();
}

void MyWidget::mousePressEvent(QMouseEvent *event)
{
    if(event->button()==Qt::LeftButton){
        offset = event->globalPos() - pos();
        m_Pressed = true;
    }
}

void MyWidget::mouseMoveEvent(QMouseEvent *event)
{
    if(event->buttons()&Qt::LeftButton){
        QPoint temp;
        temp = event->globalPos() - offset;
        move(temp);
    }
}

void MyWidget::mouseReleaseEvent(QMouseEvent *event)
{
    if(event->button()==Qt::LeftButton){
        m_Pressed = false;
    }
}
